package com.meilai.project.controller.web.personnel.onTurn;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.personnel.onTurn.EntryApplyCheckDTO;
import com.meilai.project.dto.personnel.onTurn.EntryApplyDTO;
import com.meilai.project.dto.personnel.onTurn.EntryListSearchDTO;
import com.meilai.project.service.personnel.onTurn.EntryApplyService;
import com.meilai.project.vo.personnel.onTurn.EntryApplyVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;

@Api(value = "考察登记控制器", tags = {"考察登记"})
@RestController
@RequestMapping("api/web/entry_apply")
public class WebEntryApplyController {

    @Autowired
    private EntryApplyService service;

    @ApiOperation(value = "列表分页查询", notes = "人事，查看所有考察登记记录", response = EntryApplyVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("list")
    public CommonResult list(
            @ApiParam(name = "page", value = "当前页", required = true)
            @RequestParam(value = "page", defaultValue = "1", required = true) Integer page,
            @ApiParam(name = "page_size", value = "每页数量", required = true)
            @RequestParam(value = "page_size", defaultValue = "10", required = true) Integer page_size,
            @ApiParam(name = "flow_status", value = "当前状态", required = false)
            @RequestParam(value = "flow_status", required = false) Integer flow_status,
            @ApiParam(name = "name", value = "姓名", required = false)
            @RequestParam(value = "name", required = false) String name,
            @ApiParam(name = "id_no", value = "身份证号码", required = false)
            @RequestParam(value = "id_no", required = false) String id_no,
            @ApiParam(name = "mobile", value = "联系电话", required = false)
            @RequestParam(value = "mobile", required = false) String mobile,
            @ApiParam(name = "dispatch", value = "接受工作调度 0：否  1：是", required = false)
            @RequestParam(value = "dispatch", required = false) Integer dispatch,
            @ApiParam(name = "work_overtime", value = "接受加班 0：否  1：是", required = false)
            @RequestParam(value = "work_overtime", required = false) Integer work_overtime,
            @ApiParam(name = "business_trip", value = "接受出差 0：否  1：是", required = false)
            @RequestParam(value = "business_trip", required = false) Integer business_trip,
            @ApiParam(name = "employed", value = "曾经被应聘 0：否  1：是", required = false)
            @RequestParam(value = "employed", required = false) Integer employed,
            @ApiParam(name = "employed_friends", value = "有亲属或朋友在我司工作 0：否  1：是", required = false)
            @RequestParam(value = "employed_friends", required = false) Integer employed_friends,
//            @ApiParam(name = "comp_fill_probation_start_min", value = "考察期开始时间，时间段搜索的起始，yyyy-MM-dd", required = false)
//            @RequestParam(value = "comp_fill_probation_start_min", required = false) String comp_fill_probation_start_min,
//            @ApiParam(name = "comp_fill_probation_start_max", value = "考察期开始时间，时间段搜索的结束，yyyy-MM-dd", required = false)
//            @RequestParam(value = "comp_fill_probation_start_max", required = false) String comp_fill_probation_start_max,
//            @ApiParam(name = "comp_fill_probation_end_min", value = "考察期结束时间，时间段搜索的起始，yyyy-MM-dd", required = false)
//            @RequestParam(value = "comp_fill_probation_end_min", required = false) String comp_fill_probation_end_min,
//            @ApiParam(name = "comp_fill_probation_end_max", value = "考察期结束时间，时间段搜索的结束，yyyy-MM-dd", required = false)
//            @RequestParam(value = "comp_fill_probation_end_max", required = false) String comp_fill_probation_end_max,
            @ApiParam(name = "comp_fill_probation_min", value = "考察期最小值，时间段搜索的起始，yyyy-MM-dd", required = false)
            @RequestParam(value = "comp_fill_probation_min", required = false) String comp_fill_probation_min,
            @ApiParam(name = "comp_fill_probation_max", value = "考察期最大值，时间段搜索的结束，yyyy-MM-dd", required = false)
            @RequestParam(value = "comp_fill_probation_max", required = false) String comp_fill_probation_max,
            @ApiParam(name = "comp_fill_probation_salary_min", value = "考察期工资，最小值", required = false)
            @RequestParam(value = "comp_fill_probation_salary_min", required = false) BigDecimal comp_fill_probation_salary_min,
            @ApiParam(name = "comp_fill_probation_salary_max", value = "考察期工资，最大值", required = false)
            @RequestParam(value = "comp_fill_probation_salary_max", required = false) BigDecimal comp_fill_probation_salary_max,
            @ApiParam(name = "comp_fill_salary_min", value = "入编后工资，最小值", required = false)
            @RequestParam(value = "comp_fill_salary_min", required = false) BigDecimal comp_fill_salary_min,
            @ApiParam(name = "comp_fill_salary_max", value = "入编后工资，最大值", required = false)
            @RequestParam(value = "comp_fill_salary_max", required = false) BigDecimal comp_fill_salary_max) {

        EntryListSearchDTO searchDTO = new EntryListSearchDTO(flow_status, name, id_no, mobile, dispatch, work_overtime, business_trip,
                employed, employed_friends, comp_fill_probation_min, comp_fill_probation_max,
                comp_fill_probation_salary_min, comp_fill_probation_salary_max,
                comp_fill_salary_min, comp_fill_salary_max);

        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }

    @ApiOperation(value = "详情", notes = "详情", response = EntryApplyVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("detail/{id}")
    public CommonResult detail(@ApiParam(name = "id", value = "数据id", required = true) @PathVariable("id") Long id) {
        return new CommonResult().success(service.getOneById(id));
    }

    @ApiOperation(value = "保存", notes = "保存")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("save")
    public CommonResult save(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) EntryApplyDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.save(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "提交", notes = "提交")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("begin")
    public CommonResult begin(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) EntryApplyDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.begin(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "通过", notes = "通过")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PutMapping("pass")
    public CommonResult pass(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) EntryApplyCheckDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.pass(dto)) {
                return new CommonResult().success("通过成功");
            } else {
                return new CommonResult().failed("通过失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "驳回", notes = "驳回")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PutMapping("return_back")
    public CommonResult returnBack(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) EntryApplyCheckDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.returnBack(dto)) {
                return new CommonResult().success("驳回成功");
            } else {
                return new CommonResult().failed("驳回失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("delete")
    public CommonResult delete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.delete(id)) {
                return new CommonResult().success("删除成功");
            } else {
                return new CommonResult().failed("删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "彻底删除", notes = "彻底删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("actual_delete")
    public CommonResult actualDelete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.actualDelete(id)) {
                return new CommonResult().success("彻底删除成功");
            } else {
                return new CommonResult().failed("彻底删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }
}
